package pl.koszolko.documentclustering.cluster.document;

import java.util.Set;

import org.apache.log4j.Logger;

import pl.koszolko.documentclustering.dao.Document;

public class DocumentClusteringByReferences extends DocumentForClustering {
	private static Logger logger = Logger.getLogger(DocumentClusteringByReferences.class);
	
	private static final long serialVersionUID = 1L;

	public DocumentClusteringByReferences(Document document) {
		super(document);
	}
	
	@Override
	public synchronized double countSimilarity(DocumentForClustering document) {
		if(document instanceof DocumentClusteringByReferences)
			return countSimilarityUsingReferences((DocumentClusteringByReferences) document);
		throw new RuntimeException("Niepoprawny typ DocumentForClustering");
	}
	
	private double countSimilarityUsingReferences(DocumentClusteringByReferences document) {
		logger.debug("Start countSimilarityUsingReferences");
		logger.debug("d1 = " + getTitle());
		logger.debug("d2  - " + document.getTitle());
		
		double allReferences = 0;
		double wspolneREferences = 0;
		
		allReferences = getReferencesSize();
		
		for(Document docRef : document.getReferences()) {
			if(ifReferencesContains(docRef)) {
				++wspolneREferences;
			} else {
				++allReferences;
			}
		}
		
		logger.debug("wspolne referencje = " + wspolneREferences);
		logger.debug("wszystkie referencje = " + allReferences);
		if(allReferences == 0)
			return 0D;
		else
			return wspolneREferences/allReferences;
	}
}
